package app

import (
	"cli-desktop/proto"
	"cli-desktop/public"
)

/*
*  -*- coding:utf8 -*-
*
*  用户操作相关
*
*  @Author  : chengms
*  @WebSite : chengms.com
*  @Email   : chengms2012@163.com
*  @TIME    : 2020/8/19 11:47
*  @File    : userOp.go
 */

const (
	StsLoginTrue         = 1 // 登录成功
	StsLoginFail         = 2 // 登录失败
	StsRegisterTrue      = 3 // 注册成功
	StsRegisterNameFail  = 4 // 注册失败, 用户名被占用
	StsRegisterEmailFail = 5 // 邮箱已经被注册
	StsLoginOut          = 6 // 退出
	StsOffLine           = 7 // 离线
	StsUnusual           = 8 // 用户状态异常
)

type UserOpSts struct {
	UserId     int    `json:"userId"`
	UserStatus int    `json:"userStatus"`
	Msg        string `json:"Msg"`
}

type UserInfo struct {
	UserId     int    `json:"userId"`
	UserName   string `json:"userName"`
	UserPasswd string `json:"userPasswd"`
	Email      string `json:"Email"`
	UserStatus int    `json:"userStatus"`
	FriendCnt  int    `json:"FriendCnt"` // 好友数量
}

type FriendInfo struct {
	UserId     int    `json:"userId"`
	UserName   string `json:"userName"`
	Email      string `json:"Email"`
	UserStatus int    `json:"userStatus"`
}

type UserOption struct {
	Info      UserInfo
	Friend    map[string]*FriendInfo
	FriendCnt int
	ClientSts chan int // 登录、注册后等接收服务端消息
}

func (u *UserOption) SetUserMsg(name, passwd string) {
	u.Info.UserName = name
	u.Info.UserPasswd = passwd
	u.Info.Email = "Unknown"
	u.Info.UserStatus = 0
}

func (u *UserOption) LoginMsg(name, passwd string) {
	u.SetUserMsg(name, passwd)
}

func (u *UserOption) ReceiveLoginMsg(msg []string) {
	resp := UserOpSts{}
	public.Loggerf.Debugf("Resp: %v", msg)
	_ = public.Jsonit.UnmarshalFromString(msg[0], &resp)
	UserOp.Info.UserId = resp.UserId
	UserOp.ClientSts <- resp.UserStatus
}

func (u *UserOption) RegisterMsg(name, passwd, email string) {
	u.SetUserMsg(name, passwd)
	u.Info.Email = email
}

func (u *UserOption) ReceiveRegisterMsg(msg []string) {
	resp := UserOpSts{}
	public.Loggerf.Debugf("Resp: %v", msg)
	_ = public.Jsonit.UnmarshalFromString(msg[0], &resp)
	UserOp.Info.UserId = resp.UserId
	UserOp.ClientSts <- resp.UserStatus
	return
}

var UserOp = UserOption{
	Friend:    make(map[string]*FriendInfo),
	FriendCnt: 0,
	ClientSts: make(chan int),
}

func UserLogin(name, passwd string) (response int, err error) {
	response = StsLoginFail
	UserOp.LoginMsg(name, passwd)

	sendCmd := proto.NewSendCommand(proto.CmdLogin)
	err = sendCmd.SendCmd(UserOp.Info)
	if err != nil {
		public.Loggerf.Error(err)
		return
	}

	response = <-UserOp.ClientSts
	if response != StsLoginTrue {
		return
	}

	response = StsLoginTrue

	return
}

func UserRegister(name, passwd, email string) (response int, err error) {
	response = StsRegisterNameFail
	UserOp.RegisterMsg(name, passwd, email)

	sendCmd := proto.NewSendCommand(proto.CmdRegister)
	err = sendCmd.SendCmd(UserOp.Info)
	if err != nil {
		public.Loggerf.Error(err)
		return
	}

	response = <-UserOp.ClientSts
	if response != StsRegisterTrue {
		if response == StsRegisterNameFail {
			public.Loggerf.Info("用户名已存在")
		} else if response == StsRegisterEmailFail {
			public.Loggerf.Info("邮箱已被注册")
		}
		return
	}

	// public.Loggerf.Info("注册成功")
	response = StsRegisterTrue

	return
}
